<?php
class main_curl {
	var $url, $post, $cookie, $filename, $referer, $proxy;
	var $mm_url, $mm_post, $mm_coooki, $mm_filename, $mm_referer, $mm_proxy;
	var $mm_failed_arr, $mm_failed_trigger;

	function method_main_curl($sleep_time=1500000) {
		global $is_debug;
		usleep($sleep_time);
		$ch = curl_init();
		if($this->proxy !== FALSE) {
			curl_setopt($ch, CURLOPT_PROXY,      $this->proxy);
			curl_setopt($ch, CURLOPT_PROXYTYPE,  CURLPROXY_HTTP);
			//curl_setopt($ch, CURLOPT_PROXYTYPE,  CURLPROXY_SOCKS5);
			echo "proxy=".$this->proxy."\n";
		}
		curl_setopt($ch, CURLOPT_FAILONERROR,    1);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
		curl_setopt($ch, CURLOPT_TIMEOUT,        15);
		curl_setopt($ch, CURLOPT_URL,            $this->url);
		if ($this->post !== FALSE) {
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $this->post);
		}
		curl_setopt($ch, CURLOPT_COOKIEJAR,      $this->cookie);
		curl_setopt($ch, CURLOPT_COOKIEFILE,     $this->cookie);
		curl_setopt($ch, CURLOPT_USERAGENT,      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");

		ob_start();
		curl_exec ($ch);
		curl_close ($ch);
		$result = ob_get_contents();
		ob_end_clean();

		if(strcmp($result, "") != 0) {
			$fd = fopen($this->filename, "w");
			fwrite($fd, $result);
			fclose($fd);
			return TRUE;
		} else {
			echo ">>>=== ".date("Y-m-d H:i:s")." CURL FAILED ===<<<\n";
			return FALSE;
		}
	}

	function mm_method_main_curl($sleep_time=1100000, $mm_sleep_time=25000) {
		global $is_debug;
		usleep($sleep_time);
			
		$mh = curl_multi_init();
		for($t=0; $t < count($this->mm_url); $t++) {
			$ch[$t] = curl_init();
			//*************************************** P R O X Y *************************************
			if($this->mm_proxy[$t] !== FALSE) {
				curl_setopt($ch[$t], CURLOPT_PROXY,      $this->mm_proxy[$t]);
				curl_setopt($ch[$t], CURLOPT_PROXYTYPE,  CURLPROXY_HTTP);
				echo "proxy=".$this->mm_proxy[$t]."\n";
			}
			/***************************************************************************************/
			curl_setopt($ch[$t], CURLOPT_FAILONERROR,    1);
			curl_setopt($ch[$t], CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch[$t], CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch[$t], CURLOPT_TIMEOUT,        15);
			curl_setopt($ch[$t], CURLOPT_URL,            $this->mm_url[$t]);
			if ($this->mm_post[$t] !== FALSE) {
				curl_setopt($ch[$t], CURLOPT_POST,       1);
				curl_setopt($ch[$t], CURLOPT_POSTFIELDS, $this->mm_post[$t]);
			}
			curl_setopt($ch[$t], CURLOPT_COOKIEJAR,      $this->mm_cookie[$t]);
			curl_setopt($ch[$t], CURLOPT_COOKIEFILE,     $this->mm_cookie[$t]);
			curl_setopt($ch[$t], CURLOPT_USERAGENT,      "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
			curl_multi_add_handle ($mh, $ch[$t]);
		}

		do {
			$n = curl_multi_exec($mh, $active);
			usleep($mm_sleep_time); // чтобы не грузить сразу проц
		} while ($active > 0);

		for($t=0; $t < count($this->mm_url); $t++) {
			$mm_result_arr[$t] = curl_multi_getcontent($ch[$t]);
			/*
			 $mm_info_arr[$t] = curl_getinfo($ch[$t]);
			 $code_arr[$t] = $mm_info_arr['http_code'];
			 $time_arr[$t] = $mm_info_arr['total_time'];
			 */
			curl_multi_remove_handle($mh, $ch[$t]);
			curl_close($ch[$t]);
		}
		curl_multi_close($mh);

		$this->mm_failed_trigger          = 0;
		unset($this->mm_failed);
		for($t=0; $t < count($this->mm_url); $t++) {
			if(strcmp($mm_result_arr[$t], "") != 0) {
				$mm_fd[$t] = fopen($this->mm_filename[$t], "w");
				fwrite($mm_fd[$t], $mm_result_arr[$t]);
				fclose($mm_fd[$t]);
				if($is_debug) {
					echo ">>> ".$t." URL=". $this->mm_url[$t] ."\t".date("Y-m-d H:i:s")." MULTICURL OK\n";
					echo ">>> ".$t." POST=".$this->mm_post[$t]."\t".date("Y-m-d H:i:s")." MULTICURL OK\n";
				}
				$this->mm_failed[]        = 0;
			} else {
				if($is_debug) {
					echo ">>> ".$t." URL=". $this->mm_url[$t] ."\t".date("Y-m-d H:i:s")." MULTICURL FAILED<<<\n";
					echo ">>> ".$t." POST=".$this->mm_post[$t]."\t".date("Y-m-d H:i:s")." MULTICURL FAILED<<<\n";
					print_r($this->mm_filename);
				}
				#---
				$this->mm_failed[]        = 1;
				$this->mm_failed_trigger  = 1;
				#---
			}
		}
		#print_r($this->mm_failed);

		###
		/*
		for($t=0; $t < count($this->mm_url); $t++) {
		if($this->mm_failed[$t] == 0) {
		$this->mm_failed_trigger  = 0;
		} else {
		$this->mm_failed_trigger  = 1;
		break;
		}
		}
		*/
	}
}

?>